import {Store} from 'iflux2';
import webApi from '../receipt-common/webApi';
import {fromJS} from 'immutable';
import {message} from 'antd';

import OrderListActor from './actor/order-list-actor';
import PaginationActor from 'actor/pagination';
import {OrderListType} from './constant/action-type';
import { PaginationType } from 'actor/pagination/type';

export default class AppStore extends Store {
    bindActor() {
        return [
            new OrderListActor,
            new PaginationActor
        ]
    }


    queryOrderList = async (param = {})=>{
        const allState = this.state();

        const params = Object.assign({}, {
            pageNum: allState.getIn(['pagination', 'pageNum']) - 1,
            pageSize: allState.getIn(['pagination', 'pageSize']),
            orderStatusEnum: "GRP_WAIT_RECEIVE"
        }, param);

        try {
            const {userLogos, orderPageList: {dataList, ...rest}} = await webApi.queryOrderList(params);
            this.dispatch(OrderListType.SET_ORDER_LIST, {
                list: fromJS(dataList || []),
                userLogos: fromJS(userLogos)
            });
            rest.pageNum += 1;
            this.dispatch(PaginationType.CHANGE_PAGE_PARAM, fromJS(rest));
        } catch (err) {
            return Promise.reject(err);
        }
    }


    receipt = async (tid = "")=>{
        try {
            await webApi.receipt(tid);
        } catch (err) {
            return Promise.reject(err);
        }
    }


    completeOrder = async(tid = "")=>{
        try {
            await webApi.completeOrder(tid);
        } catch (err) {
            return Promise.reject(err);
        }
    }


    changePageCode = (pagination)=>{
        this.dispatch(PaginationType.CHANGE_PAGE_PARAM, fromJS(pagination))
    }


    resetPagination = ()=>{
        this.dispatch(PaginationType.RESET_PAGINATION_PARAM);
    }
}